package funnytrails;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;

public class LeftFrame extends WebPage
{
    /**
     * Link that, when clicked, changes the frame target's frame class (and as that is a shared
     * model which is also being used by the 'master page' {@link BodyFrame}, changes are
     * immediately reflected) and set the response page to the top level page {@link BodyFrame}.
     * Tags that use this link should have a <code>target="_parent"</code> attribute, so that the
     * top frame will be refreshed.
     */
    private static final class ChangeFramePageLink extends Link
    {
        private static final long serialVersionUID = 1L;

        /** parent frame class. */
        private final BodyFrame bodyFrame;

        /** this link's target. */
        private final Class<? extends Page> pageClass;

        /**
         * Construct.
         * 
         * @param <C>
         * 
         * @param id
         * @param bodyFrame
         * @param pageClass
         */
        public <C extends Page> ChangeFramePageLink(String id, BodyFrame bodyFrame,
            Class<C> pageClass)
        {
            super(id);
            this.bodyFrame = bodyFrame;
            this.pageClass = pageClass;
        }

        /**
         * @see org.apache.wicket.markup.html.link.Link#onClick()
         */
        @Override
        public void onClick()
        {
            // change frame class
            bodyFrame.getFrameTarget().setFrameClass(pageClass);

            // trigger re-rendering of the page
            setResponsePage(bodyFrame);
        }
    }

    private static final long serialVersionUID = 1L;

    /**
     * Constructor
     * 
     * @param index
     *            parent frame class
     */
    public LeftFrame(BodyFrame index)
    {
        add(new ChangeFramePageLink("events", index, EventsPage.class));
        add(new ChangeFramePageLink("trails", index, TrailsPage.class));
        add(new ChangeFramePageLink("guides", index, GuidesPage.class));
    }

    /**
     * No need for versioning this frame.
     * 
     * @see org.apache.wicket.Component#isVersioned()
     */
    @Override
    public boolean isVersioned()
    {
        return false;
    }
}
